#ifndef __IS_CORE_HALTONSEQUANCE__
#define __IS_CORE_HALTONSEQUANCE__

namespace IS
{

template<typename REAL>
class HaltonSequence
{
public:
	HaltonSequence(REAL base = 13){value = 0; inv_base = (REAL)1.0 / base;}

	REAL value, inv_base;

	void number( long i, int base )
	{
		REAL f = inv_base = (REAL)1.0 / base;
		value = (REAL)0.0;
		while ( i > 0 ) {
			value += f * (REAL)(i % base);
			i /= base;
			f *= inv_base;
		}
	}

	void next( )
	{
		REAL r = (REAL)1.0 - value - (REAL)0.0000000001;
		if (inv_base < r)
		{
			value += inv_base;
		}
		else
		{
			REAL h = inv_base, hh;
			do
			{
				hh = h;
				h *= inv_base;
			} while ( h >= r );
			value += hh + h - (REAL)1.0;
		}
	}

	operator REAL() { return value; }

	REAL getNext(){next(); return value;}
	void reset(){value = 0;};
};

}	// namespace IS

#endif	// __IS_CORE_HALTONSEQUANCE__